﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HolisticWare.VoIP.Getafisk.EXE_01PL_05netfx30.SL30.any_any_any.UserControls
{
	public partial class SilverlightControlWebService : UserControl
	{
		public SilverlightControlWebService()
		{
			InitializeComponent();

			return;
		}

		private void buttonWebServiceTestCall_Click(object sender, RoutedEventArgs e)
		{
			this.CallWebServiceGetafiskDataBaseDiagnostics();

			return;
		}

		# region    CallWebServiceGetafiskDataBaseDiagnostics
		//-------------------------------------------------------------------------
		// WebService Uri path relaltive if on the same server which hosts 
		// Silverlight application!
		private
			string
			ws_uri_path = @"../WebServiceGetafisk.asmx"
			;

		// Uri of the WebService (asmx file)
		private
			Uri
			uri_ws
			;

		// WebService client/proxy
		private
			ServiceReferenceGetafisk.WebServiceGetafiskSoapClient
			client_proxy
			;


		// Result of the Web Service Method
		public
			string
			WebServiceGetafiskDataBaseDiagnosticsResult
			;
		public
			void
			CallWebServiceGetafiskDataBaseDiagnostics
			(

			)
		{
			# region    initialization
			//-------------------------------------------------------------------------
			string web_service_uri_relative_asmx = ws_uri_path;
			// this url will work both in dev and after deploy.
			uri_ws = new Uri
							(
							  Application.Current.Host.Source
							, web_service_uri_relative_asmx
							);
			// int port = HtmlPage.Document.DocumentUri.Port;
			//-------------------------------------------------------------------------
			# endregion initialization

			System.ServiceModel.EndpointAddress endpoint;
			endpoint = new System.ServiceModel.EndpointAddress(uri_ws);

			string client_binding_config_name = "WebServiceGetafiskSoap";
			client_proxy =
				new ServiceReferenceGetafisk.WebServiceGetafiskSoapClient
												(
												  client_binding_config_name
												);
			client_proxy.Endpoint.Address =
							new System.ServiceModel.EndpointAddress(uri_ws);

			// This is the default constructor, url will be read from the 
			// client config file binding attribute
			/*
			client = new ServiceReferenceTest.WebServiceTestSoapClient
								(
								  client_binding_config_name
								 , address.AbsolutePath
								 );
			*/

			//async callback when completed
			client_proxy.DataBaseDiagnosticsCompleted +=
				new EventHandler<ServiceReferenceGetafisk.DataBaseDiagnosticsCompletedEventArgs>
							(
							  client_WebServiceGetafiskDataBaseDiagnosticsCompleted
							);
			// web service call
			client_proxy.DataBaseDiagnosticsAsync();

			return;
		}

		/// <summary>
		/// client_WebServiceGetafiskDataBaseDiagnosticsCompleted
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private
			void
			client_WebServiceGetafiskDataBaseDiagnosticsCompleted
			(
			  object sender
			, ServiceReferenceGetafisk.DataBaseDiagnosticsCompletedEventArgs e
			)
		{
			string response = e.Result;

			// DataBind
			textBoxTestResponse.Text = response;

			return;
		}
		//-------------------------------------------------------------------------
		# endregion CallWebServiceGetafiskDataBaseDiagnostics

	}
}
